//
//  QDLargeRefreshTitleViewController.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/2/28.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 集成 QDLargeTitleTableHeaderView 滚动并且支持下拉刷新的控制器
class QDLargeRefreshTitleViewController: NWTableViewController,QDLargeTitleTableHeaderScrollable {

    /// tableView的标题headerView
    var largeTableHeaderView = QDLargeTitleTableHeaderView()
    /// 大标题视图的占位图
    let placeTableHeaderView = UIView()
    /// 大标题视图的高度
    var largeHeaderHeight: CGFloat = QDLargeTitleTableHeaderViewNormalHeight + 2
    /// 子类调用方法
    func tableViewDidScroll(isShow: Bool){}
    
    override func loadPropertysAtInitialization() {
        super.loadPropertysAtInitialization()
        
        tableView.rowHeight = 88
        tableView.showsVerticalScrollIndicator = true
        tableView.scrollIndicatorInsets.top = largeHeaderHeight
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        // 修改header
        if headerRefreshEnable {
            tableView.mj_header.ignoredScrollViewContentInsetTop = -1 * largeHeaderHeight            
        }
        
        // 初始化largeTableHeaderView
        largeTableHeaderView.titleLabel.text = "地铁乘车记录"
        largeTableHeaderView.backgroundColor = .white
        largeTableHeaderView.frame = CGRect(origin: CGPoint(x: 0, y:  0), size: CGSize(width: ritl_width, height: largeHeaderHeight))
        
        // 追加视图
        placeTableHeaderView.frame = largeTableHeaderView.frame;
        placeTableHeaderView.ritl_originY = 0
        placeTableHeaderView.backgroundColor = .clear
        tableView.tableHeaderView = placeTableHeaderView
        tableView.addSubview(largeTableHeaderView)
    }
    
    
    override func viewDidLayoutSubviews() {
        largeTableHeaderView.frame = CGRect(origin: CGPoint(x: 0, y:  0), size: CGSize(width: ritl_width, height: largeHeaderHeight))
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        setNavigationBarShadowViewHidden(navigationItem.title == nil)
    }
    
    
    // MARK: NavigationBar
    override var nav_mutableBarShadowHidden: Bool { return true }
    /// 背景色
    override var tableViewBackgroundColor: UIColor { return .white }

    
    override func headerRefreshBeginHandler() {
        DispatchQueue.global().async {
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2){
                self.endHeaderRefreshing()
            }
        }
    }
    
    
    //MARK: ScrollViewDelegate
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        /// navigationItem
        tableViewDidScroll(contentOfset: scrollView.contentOffset) { (isShow) in
            setNavigationBarShadowViewHidden(!isShow)
            tableViewDidScroll(isShow: isShow)
        }
        
        let originOffsetY = -1 * BarDistance.NavigationBar.height
        let offsetY = scrollView.contentOffset.y
        if offsetY <= originOffsetY {//往下拉，但是保持
            largeTableHeaderView.ritl_originY = offsetY - originOffsetY
        }
    }
}
